/*高度自适应*/
var height = $(window).height() - 30;
$("#map_center").height(height);

var map_show = false;//点击推荐路线是否响应
var all_markers=[];//返回的8个坐标
var map = new BMap.Map("l-map");

var start = {
    long: parseFloat($('.gis_map h5').attr('data-long')),
    lat: parseFloat($('.gis_map h5').attr('data-lat')),
    label:"暂无"
};
if($('.gis_map h5').attr('data-label')){
    start.label = $('.gis_map h5').attr('data-label')
}
//var startpoint = new BMap.Point(start.long, start.lat);
var ggPoint = new BMap.Point(start.long, start.lat);
var startpoint;

//坐标转换完之后的回调函数
translateCallback = function (point){
    startpoint = point;
    map.centerAndZoom(startpoint, 14);
    var start_marker = returnFirstMarker(startpoint);//建立当前位置坐标
    map.addOverlay(start_marker);  //地图添加当前位置坐标

    //访问服务器，初始化标签坐标
    $.getJSON("index.php?s=/addon/StoreGis/StoreGis/getShopJson").success(function(data){
        init_Marker(start_marker, data);
    });
};

BMap.Convertor.translate(ggPoint,2,translateCallback);


map.enableScrollWheelZoom();
//map.addControl(new BMap.ScaleControl({offset:new BMap.Size(100,100)}));//比例尺控件
map.addControl(new BMap.MapTypeControl());//地图，卫星，三维
var opts={offset:new BMap.Size(10,10)}; /**控件位置偏移*/
map.addControl(new BMap.NavigationControl());

var driving = new BMap.DrivingRoute(map, {renderOptions: {map: map, panel: "r-result", autoViewport: true}});

var show=false;   //显示与隐藏
$(".btn_center").click(function(){
    if((!show)&&map_show){
        $(".map_road").show();
        show=true;
    }
    else if(show&&map_show){
        $(".map_road").hide();
        show=false;
    }
});


function returnFirstMarker(startpoint){  /*返回当前位置坐标*/
    var myIcon = new BMap.Icon("Addons/StoreGis/View/default/Public/images/arrow.png", new BMap.Size(30, 30));
    var start_marker = new BMap.Marker(startpoint, {icon:myIcon});//当前位置
    start_marker.setAnimation(BMAP_ANIMATION_DROP);
    var opts = {width : 250};
    var content="<div>\n    <p>\n        <b>当前位置</b>\n    </p>\n    <p>\n        <b>地址：</b>\n        <span>" + start.label + "</span>\n    </p>\n</div>";
    var infoWindow = new BMap.InfoWindow(content, opts);
    start_marker.addEventListener('click',function(){
        map.openInfoWindow(infoWindow, startpoint);
    });
    map.openInfoWindow(infoWindow, startpoint);
    return start_marker;
}

/*消息窗口*/
function getinfoWindow(index, shop){
    var opts = {width : 250};
    var content="<div>\n    <p>\n        <b>当前位置</b>\n    </p>\n    <p>\n        <b>名称：</b>\n        <span>" + shop.name + "</span>\n    </p>\n    <p>\n        <b>地址：</b>\n        <span>" + shop.address + "</span>\n    </p>\n    <p>\n        <b>电话：</b>\n        <span>" + shop.tel + "</span>\n    </p>\n    <p>\n        <b>标签：</b>\n        <span>标签1、2、3、4</span>\n    </p>\n    <p>\n        <b>简介：</b>\n        <span>" + shop.fullname + "</span>\n    </p>\n    <p>\n        <button onclick=\'show_road(" + index + ")'>到这里去</button>\n    </p>\n</div>";
    var infoWindow = new BMap.InfoWindow(content, opts);
    return infoWindow;
}

//显示路径
function show_road(id){
    var marker= all_markers[id];
    driving.search(startpoint, marker.point);    //显示一条公交线
    map_show=true;//允许显示路线
}

 function data_sqrt(start_marker,datas){  //获取data与坐标的距离点赋值处理
     var  r_datas=[];
     $.each(datas,function(index,data){
         var less_lng= Math.abs(start_marker.point.lng - data.longitude) * Math.abs(start_marker.point.lng - data.longitude);
         var less_lat= Math.abs(start_marker.point.lat - data.latitude) * Math.abs(start_marker.point.lat - data.latitude);
         data.sqrt_sum=less_lat+less_lng;
         r_datas.push(data);
     });
    return r_datas;
 }

//坐标排序
 function data_sort(r_datas){
     for(var i=0;i<(r_datas.length-1);i++)
     {
         var pos=i;
         var max=r_datas[i+1];
         while((pos>=0)&&(max.sqrt_sum<r_datas[pos].sqrt_sum))
         {
             r_datas[pos+1]=r_datas[pos];
             pos=pos-1;
         }
         r_datas[pos+1]=max;
     }
    var data_sorted = r_datas.slice(0,8);
    return data_sorted;
 }
 function init_Marker(start_marker, datas){ //初始化商店坐标
      var r_data = data_sqrt(start_marker,datas);
      var result = data_sort(r_data);
      var myIcon = new BMap.Icon("Addons/StoreGis/View/default/Public/images/law.png", new BMap.Size(19, 25));
      $.each(result,function(index,shop){
          var sggPoint = new BMap.Point(shop.longitude,shop.latitude);
          BMap.Convertor.translate(sggPoint,2,function(point){
              var marker = new BMap.Marker(point,{icon:myIcon});//当前位置
              all_markers[index] = marker;
              marker.info = getinfoWindow(index, shop);
              marker.addEventListener('click',function(){
                  map.openInfoWindow(marker.info,marker.point);
              });
              map.addOverlay(marker);
              marker.setAnimation(BMAP_ANIMATION_DROP);
          });

      });
  }

